﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;

namespace TestWeb.Helpers
{
    /// <summary>
    /// Class for implementing extension methods
    /// </summary>
    public static class StringHelper
    {
        /// <summary>
        /// Within a specified input string, replaces all strings that match a specified
        /// regular expression with a specified replacement string.
        /// </summary>
        /// <param name="s">The string to search for a match.</param>
        /// <returns></returns>
        public static string StripTags(this string s)
        {
            return Regex.Replace(s, "<.*?>", string.Empty);
        }

        /// <summary>
        /// TrimStart with string parameter
        /// </summary>
        /// <param name="s"></param>
        /// <param name="trimChars"></param>
        /// <returns></returns>
        public static string TrimStart(this string s, string trimChars)
        {
            return s.TrimStart(trimChars.ToCharArray());
        }

        /// <summary>
        /// Truncate a string with specified length
        /// </summary>
        /// <param name="s"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string Truncate(this string s, int length)
        {
            if (s.Length > length)
                s = s.Substring(0, length);

            int lastPos = s.LastIndexOf(" ");
            if (lastPos == -1)
                return "...";

            return s.Substring(0, lastPos) + "...";
        }

        /// <summary>
        /// Remove specified characters
        /// </summary>
        /// <param name="s"></param>
        /// <param name="chars"></param>
        /// <returns></returns>
        public static string RemoveInvalidChars(this string s, params char[] chars)
        {
            foreach (char c in chars)
                s = s.Replace(c.ToString(), string.Empty);
            return s;
        }
    }
}